from apps.codetable.models import CodeTableDetail


def field_codetable(field, localization='zh-hans', model=None)->dict:
    """
    Args:
        field: 字段名
        localization: 语言，默认为中文
        model: 模型，默认为空

    Returns: 按语言，model和字段的字典信息, 返回类型是一个字典
    """

    try:
        code_detail = CodeTableDetail.objects.filter(codetable_head__localization=localization,
                                                     codetable_head__field__exact=field,
                                                     codetable_head__model__exact=model
                                                     ).values('code', 'name')
        def toDict(code_detail):
            result = {}
            for item in code_detail:
                code = item["code"]
                name = item["name"]
                result[code] = name
            return result

        return toDict(code_detail)
    except CodeTableDetail.DoesNotExist:
        return {}